/**
 * 
 */
package belajarzk.ui.render.grid;

import org.hibernate.Session;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.sys.ExecutionsCtrl;
import org.zkoss.zul.Detail;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Window;

import belajarzk.action.maintenance.TbmroleAction;
import belajarzk.action.maintenance.helper.MenuSelecter;
import belajarzk.action.maintenance.helper.TbmroleActionHelper;
import belajarzk.common.Common;
import belajarzk.database.hibernate.HibernateUtil;
import belajarzk.database.model.Tbmrole;
import belajarzk.ui.util.MyTolbarbutton;


/**
 * @author Owner
 * 
 */
public class TbmroleRenderer implements RowRenderer {

	public synchronized static Tree createTree(final Tbmrole job,
			final Groupbox groupbox) {
		final Tree tree = new Tree();

		tree.setId("tree_" + (Math.random() * 10000000.0));
		Toolbar hbox = new Toolbar();

		hbox.setHeight("40px");

		MyTolbarbutton button = new MyTolbarbutton("Rubah Menu", "/img/new.gif");
		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				MenuSelecter.createInstance(job, groupbox);
			}

		});

		button.setParent(hbox);

		hbox.setParent(groupbox);
		tree.setParent(groupbox);
		tree.setVisible(true);
		return tree;
	}

	@Override
	public void render(Row arg0, Object arg1) throws Exception {
		final Tbmrole job = (Tbmrole) arg1;
		final Detail detail = new Detail();
		detail.setParent(arg0);

		detail.addEventListener("onOpen", new EventListener() {
			public void onEvent(Event arg0) throws Exception {
				Common.clear(detail);
				Groupbox groupbox = new Groupbox();
				groupbox.setMold("3d");
				groupbox.setWidth("100%");

				Tbmrole myjob = (Tbmrole) HibernateUtil.currentSession().load(
						Tbmrole.class, job.getRoleId());

				final Tree tree = createTree(myjob, groupbox);
				TbmroleActionHelper.createTreeMenu(groupbox, tree, myjob);
				groupbox.setParent(detail);
			}
		});

		new Label(job.getRoleId()).setParent(arg0);
		new Label(job.getRoleName()).setParent(arg0);

		Hbox hbox = new Hbox();

		hbox.setHeight("40px");
		MyTolbarbutton button = new MyTolbarbutton(Common
				.getBahasa("common_edit"), "/img/edit.gif");
		button.setOrient("vertical");
		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				TbmroleAction.resetInput(job);
				TbmroleAction.isPrepareEdit(true);
				Window window = ((Window) ExecutionsCtrl.getCurrentCtrl()
						.getCurrentPage().getVariable("addWindow"));
				window.setTitle("Rubah Data Tbmrole");
				window.setVisible(true);
				window.onModal();
			}

		});
		button.setParent(hbox);

		button = new MyTolbarbutton(Common.getBahasa("common_delete"),
				"/img/delete.gif");
		button.setOrient("vertical");
		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				// TODO Auto-generated method stub
				Messagebox.show(Common.getBahasa("delete_data_warning"), Common
						.getBahasa("question"), Messagebox.OK
						| Messagebox.CANCEL, Messagebox.EXCLAMATION,
						new EventListener() {

							@Override
							public void onEvent(Event event) throws Exception {
								int i = new Integer(event.getData().toString());
								if (i == Messagebox.OK) {
									Session session = HibernateUtil
											.currentSession();
									try {
										// session.createSQLQuery(
										// "delete from job_has_menu where job = '"
										// +job.getId()+"'").executeUpdate();

										session.delete(job);

										TbmroleAction mDeptAction = new TbmroleAction();
										mDeptAction.searchDefault();
									} catch (Exception e) {
										// TODO: handle exception
										e.printStackTrace();

										Messagebox
												.show("Data ini tidak bisa dihapus ..");
									}

								}

							}
						});

			}

		});
		button.setParent(hbox);
		hbox.setParent(arg0);
	}
}
